<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form ref="form" label-width="140px" :model="formData" :rules="formRules">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="角色权限" prop="description">
        <el-input
          v-model="formData.description"
          type="textarea"
          :rows="3"
          placeholder="请输入"
        />
      </el-form-item>
      <!-- 树 -->
      <el-form-item label="权限节点" prop="menuids">
        <el-tree
          ref="tree"
          show-checkbox
          :props="{label:'title'}"
          :data="allTreeData"
          node-key="id"
          @check="checkTree"
        />
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button
          type="primary"
          size="small"
          @click="onSubmit"
        >提交</el-button>
        <el-button size="small" @click="dialogShow = false">关闭</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { getSysMenuApi } from '@/api/sys_menu'
import { addSysRoleInfoApi, updateSysRoleInfoApi } from '@/api/sys_role'
export default {
  name: 'AddFormRos',
  data() {
    return {
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        name: '',
        description: '',
        menuids: []
      },
      // 表单验证规则
      formRules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        description: [{ required: true, message: '请输入', trigger: 'blur' }]
        // menuids: [{ validator: validatePass, message: '请输入', trigger: 'blur' }]
      },
      allTreeData: []// 树渲染的数组
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  created() {
    this.getTreeData()
  },
  methods: {
    async getTreeData() {
      const res = await getSysMenuApi()
      console.log(res)
      this.allTreeData = res.data
    },
    // 这个方法就是实现双向绑定的效果
    checkTree(nodeData, data) {
      // data中的有当前数据中的数据
      this.formData.menuids = [...data.checkedKeys, ...data.halfCheckedKeys]
      console.log(data)
    },
    // 提交行为
    async onSubmit() {
      // 提交之前获取一下树上的对勾和半勾，为了防止页面中啥也不该就直接点击确定按钮
      this.formData.menuids = [
        ...this.$refs.tree.getCheckedKeys(),
        ...this.$refs.tree.getHalfCheckedKeys()
      ]
      // 表单的校验
      // 加工处理表单数据(拷贝一份再加工)
      const checkId = this.$refs.tree.getCheckedKeys()// 对勾的数据
      const halfCheckId = this.$refs.tree.getHalfCheckedKeys()// 半勾的数据
      this.formData.menuids = [...checkId, ...halfCheckId]// 一起的数据放到menuids中
      await this.$refs.form.validate()
      // 请求的发生
      this.formData.id
        ? await updateSysRoleInfoApi(this.formData)
        : await addSysRoleInfoApi(this.formData)
      // 提示效果
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      // 关闭弹框
      this.dialogShow = false
      // 列表更新
      this.$emit('updateList')
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        name: '',
        description: '',
        menuids: []
      }
      // 移除校验
      this.$refs.form.clearValidate()
      this.$refs.tree.setCheckedKeys([])// 清空整个数组
    }

  }
}
</script>
<style scoped></style>
